package com.bdmc.web.controller.task;

import java.util.List;
import java.util.Arrays;

import com.bdmc.task.domain.bo.TaskTeamSpanBo;
import com.bdmc.task.service.ITaskTeamSpanService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import com.bdmc.task.domain.vo.TaskWorkTeamVo;
import com.bdmc.task.domain.bo.TaskWorkTeamBo;
import com.bdmc.task.service.ITaskWorkTeamService;
import com.bdmc.common.core.page.TableDataInfo;


/**
 * 作业班组控制器
 *
 * @author bdmc
 * @date 2022-03-06
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/task/team")
public class TaskWorkTeamController extends BaseController {

    private final ITaskWorkTeamService iTaskWorkTeamService;

    private final ITaskTeamSpanService spanService;

    /**
     * 查询作业班组列表
     */
    @SaCheckPermission("task:team:list")
    @GetMapping("/list")
    public TableDataInfo<TaskWorkTeamVo> list(@Validated(QueryGroup.class) TaskWorkTeamBo bo, PageQuery pageQuery) {
        return iTaskWorkTeamService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出作业班组列表
     */
    @SaCheckPermission("task:team:export")
    @Log(title = "作业班组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated TaskWorkTeamBo bo, HttpServletResponse response) {
        List<TaskWorkTeamVo> list = iTaskWorkTeamService.queryList(bo);
        ExcelUtil.exportExcel(list, "作业班组", TaskWorkTeamVo.class, response);
    }

    /**
     * 获取作业班组详细信息
     */
    @SaCheckPermission("task:team:query")
    @GetMapping("/{workTeamId}")
    public AjaxResult<TaskWorkTeamVo> getInfo(@PathVariable("workTeamId") Long workTeamId) {
        TaskWorkTeamVo workTeamVo = iTaskWorkTeamService.queryById(workTeamId);
        if(workTeamVo!=null&&workTeamVo.getWorkTeamId()>0){
            workTeamVo.setTeamSpanList(spanService.queryList(new TaskTeamSpanBo().setWorkTeamId(workTeamId)));
        }
        return AjaxResult.success(workTeamVo);
    }

    /**
     * 新增作业班组
     */
    @SaCheckPermission("task:team:add")
    @Log(title = "作业班组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody TaskWorkTeamBo bo) {
        if(bo.getDeptId()==null||bo.getDeptId()==0){
            bo.setDeptId(getDeptId());
        }
        return toAjax(iTaskWorkTeamService.insertByBo(bo));
    }

    /**
     * 修改作业班组
     */
    @SaCheckPermission("task:team:edit")
    @Log(title = "作业班组", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody TaskWorkTeamBo bo) {
        return toAjax(iTaskWorkTeamService.updateByBo(bo));
    }

    /**
     * 删除作业班组
     */
    @SaCheckPermission("task:team:remove")
    @Log(title = "作业班组" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{workTeamIds}")
    public AjaxResult<Void> remove(@PathVariable Integer[] workTeamIds) {
        return toAjax(iTaskWorkTeamService.deleteWithValidByIds(Arrays.asList(workTeamIds), true));
    }

    @GetMapping("combo")
    public AjaxResult<List<TaskWorkTeamVo>> combo(TaskWorkTeamBo bo){
        List<TaskWorkTeamVo> teamVoList = iTaskWorkTeamService.queryList(bo);
        for (TaskWorkTeamVo taskWorkTeamVo : teamVoList) {
            taskWorkTeamVo.setTeamSpanList(spanService.queryList(new TaskTeamSpanBo().setWorkTeamId(taskWorkTeamVo.getWorkTeamId())));
        }
        return AjaxResult.success(teamVoList);
    }
}
